import { createRouter, createWebHistory } from 'vue-router';
import RoleSelection from '../views/RoleSelection/RoleSelection.vue';
import MainLayout from '../views/MainLayout/MainLayout.vue';
import HomePage from '../views/HomePage/HomePage.vue';
import BillListPage from '../views/BillListPage/BillListPage.vue';
import AddPage from '../views/AddPage/AddPage.vue';
import SettingsPage from '../views/SettingsPage/SettingsPage.vue';
import StatisticsPage from '../views/StatisticsPage/StatisticsPage.vue'
const routes = [
    {
        path: '/',
        name: 'RoleSelection',
        component: RoleSelection,
    },
    {
        path: '/main',
        component: MainLayout,
        children: [
            {
                path: 'home',
                name: 'Home',
                component: HomePage,
            },
            {
                path: 'bill-list',
                name: 'BillList',
                component: BillListPage,
            },
            {
                path: 'add',
                name: 'Add',
                component: AddPage,
            },
            {
                path: 'settings',
                name: 'Settings',
                component: SettingsPage,
            },
            {
                path: '/statistics',
                name: 'Statistics',
                component: StatisticsPage
            }
        ],
    },
];

const router = createRouter({
    history: createWebHistory('/ourbills/'),  // 配置路由基础路径
    routes,
});

// 路由守卫：检查是否选择角色
router.beforeEach((to, from, next) => {
    const userId = localStorage.getItem('userId');
    if (to.path.startsWith('/main') && !userId) {
        next('/');
    } else {
        next();
    }
});

export default router;